package com.laikan.library.managers;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.laikan.library.R;
import com.laikan.library.components.BasicApplication;
import com.laikan.library.utils.AndroidUtils;
import com.laikan.library.utils.DeviceUtil;

/**
 * 全局弹出对话框的工具类
 */
public class GlobalAlertDialogTool {

    private Dialog alertDialog;
    private static GlobalAlertDialogTool instance;

    private GlobalAlertDialogTool() {

    }

    public static GlobalAlertDialogTool getInstance() {

        if (instance == null) {
            synchronized (GlobalAlertDialogTool.class) {
                if (instance == null) {
                    instance = new GlobalAlertDialogTool();
                }
            }
        }
        return instance;
    }

    /**
     * 显示隐藏的debug模式界面
     */
    public void showHiddenDebugDialog(final Activity context) {

        Builder builder = new AlertDialog.Builder(context);
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
            alertDialog = null;
        }
        alertDialog = builder.create();
        alertDialog.setCancelable(true);
        alertDialog.setCanceledOnTouchOutside(true);
        if (alertDialog != null && !alertDialog.isShowing()) {
            alertDialog.show();
        }
        LinearLayout dialogView = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.debug_dialog_panel, null);
        TextView value_app_version = (TextView)dialogView.findViewById(R.id.value_app_version);
        TextView value_sys_version = (TextView)dialogView.findViewById(R.id.value_sys_version);
        TextView value_phone_type = (TextView)dialogView.findViewById(R.id.value_phone_type);
        TextView value_imei = (TextView)dialogView.findViewById(R.id.value_imei);
        final TextView value_statistical_channel = (TextView)dialogView.findViewById(R.id.value_statistical_channel);
        final TextView value_statistical_release = (TextView)dialogView.findViewById(R.id.value_statistical_release);
        TextView value_resolution = (TextView)dialogView.findViewById(R.id.value_resolution);
        ImageView value_debug_mode = (ImageView)dialogView.findViewById(R.id.value_debug_mode);
       value_app_version.setText(AndroidUtils.getCurrentAppVer(context) + "");
        value_sys_version.setText(DeviceUtil.getAndroidVersion() + "");
        value_phone_type.setText(DeviceUtil.getDeviceType() + "");
        String imei = DeviceUtil.getImei(context) + "";
        value_imei.setText(imei + "");
        final String mChannel = DeviceUtil.getMetaDataStringValue(BasicApplication.getInstance(), "UMENG_CHANNEL");
        value_statistical_channel.setText(mChannel);
        value_statistical_release.setText(BasicApplication.IS_RELEASE ? "RELEASE" : "DEBUG");
        value_resolution.setText(AndroidUtils.getScreenWidth(context) + "x" + AndroidUtils.getScreenHeight(context));
        Bitmap bm;
        Resources res = context.getResources();
        if (BasicApplication.IS_RELEASE) {
            bm = BitmapFactory.decodeResource(res, R.drawable.switch_off);
            value_debug_mode.setImageBitmap(bm);
        }else {
            bm = BitmapFactory.decodeResource(res, R.drawable.switch_on);
            value_debug_mode.setImageBitmap(bm);
        }
        value_debug_mode.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Resources res = context.getResources();
                Bitmap bm;
                if (BasicApplication.IS_RELEASE) {
                    BasicApplication.IS_RELEASE = false;
                    bm = BitmapFactory.decodeResource(res, R.drawable.switch_on);
                    value_statistical_release.setText(BasicApplication.IS_RELEASE ? "RELEASE" : "DEBUG");
                }else {
                    BasicApplication.IS_RELEASE = true;
                    bm = BitmapFactory.decodeResource(res, R.drawable.switch_off);
                    value_statistical_release.setText(BasicApplication.IS_RELEASE ? "RELEASE" : "DEBUG");
                }

                ((ImageView)v).setImageBitmap(bm);
            }
        });
        alertDialog.getWindow().setContentView(dialogView);
        alertDialog.getWindow().setLayout(android.view.WindowManager.LayoutParams.WRAP_CONTENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
    }

    /**
     * 关闭弹出框
     */
    private void dismissChooseDialog() {

        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }
}
